<template>
  <div>
    <blog-drawor class="drawor"></blog-drawor>
    <blog-header></blog-header>
    <el-card class="blogCard">
      <h2 class="article">管理你的文章吧！</h2>
      <div class="inputQuery">
        <!-- 搜索框 -->
        <!-- <el-input placeholder="请输入内容"
                v-model="query">
        <el-button slot="append"
                   icon="el-icon-search"></el-button>
      </el-input> -->
      </div>
      <!-- 文章列表 -->
      <el-table :data="listData"
                style="width: 100%"
                border
                stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="name"
                         label="文章名称"
                         width="180"
                         header-align="center"
                         align="center">
        </el-table-column>
        <el-table-column prop="state"
                         label="状态"
                         width="180"
                         header-align="center"
                         align="center">
        </el-table-column>

        <el-table-column label="分类"
                         width="70"
                         header-align="center"
                         align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.cate_id == 1">技术</el-tag>
            <el-tag v-else-if="scope.row.cate_id == 2">笔记</el-tag>
            <el-tag v-else-if="scope.row.cate_id == 3">生活</el-tag>
            <el-tag v-else>兴趣</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="title"
                         label="概述"
                         width="380"
                         header-align="center"
                         align="center">
        </el-table-column>
        <el-table-column prop="pub_date"
                         label="更新日期"
                         width="200"
                         header-align="center"
                         align="center">
        </el-table-column>
        <el-table-column label="操作"
                         header-align="center"
                         align="center">
          <template slot-scope="scope">
            <el-button @click="update(scope.row.id)">更新😃</el-button>
            <el-button @click="deleteDate(scope.row.id)">删除😣</el-button>
          </template>
        </el-table-column>
      </el-table>

    </el-card>
  </div>
</template>

<script>
import blogHeader from './blogHeader.vue'
import blogDrawor from './blogDrawor.vue'
export default {
  name: 'Mg',
  components: { blogDrawor, blogHeader },

  data () {
    return {
      listData: [],
      // 查询对象
      query: ''
    }
  },
  // 生命周期函数
  created () {
    this.getList()
  },
  // 处理函数
  methods: {
    // 获取文章列表数据
    async getList () {
      const { data: res } = await this.$http.get('/my/article/list')
      if (res.status !== 0) return this.$message.error('获取文章列表数据失败')
      this.listData = res.data
    //   console.log(this.listData)
    },
    // 更新文章
    update (id) {
      window.sessionStorage.setItem('id', id)
      this.$router.push('/update')
    },
    deleteDate (id) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.get('/my/article/delete/' + id)
        if (res.status !== 0) return this.$message.error('删除文章失败！')
        this.$message.success('删除文章成功！')
        return this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="less" scoped>
.inputQuery {
  width: 200px;
  position: absolute;
  top: 55px;
}
</style>
